/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package src;

import java.util.ArrayList;

/**
 *
 * @author boomar
 */
public class IntervalCreneau {
    private int left;
    private int right;

    public IntervalCreneau(int left, int right) {
        assert (left <= right);
        this.left  = left;
        this.right = right;
    }

    public int getLeft()  { return left;  }
    public int getRight() { return right; }

    // does this interval a intersect b?
    public boolean intersects(IntervalCreneau b) {
        IntervalCreneau a = this;
        if (b.left <= a.right && b.left >= a.left) { return true; }
        if (a.left <= b.right && a.left >= b.left) { return true; }
        return false;
    }

    public boolean intersects(ArrayList<IntervalCreneau> listCreneau){
        if(listCreneau == null)
            return false;
        
        IntervalCreneau a = this;
        for (IntervalCreneau intervalCreneau : listCreneau) {
            if(intervalCreneau.intersects(a)){
                return true;
            }
        }

        return false;
    }
    
    @Override
    public String toString() {
        return "[" + left + ", " + right + "]";
    }


    // test client
    public static void main(String[] args) {
        IntervalCreneau a = new IntervalCreneau(15, 20);
        IntervalCreneau b = new IntervalCreneau(25, 30);
        IntervalCreneau c = new IntervalCreneau(10, 40);
        IntervalCreneau d = new IntervalCreneau(40, 50);

        System.out.println("a = " + a);
        System.out.println("b = " + b);
        System.out.println("c = " + c);
        System.out.println("d = " + d);

        System.out.println("b intersects a = " + b.intersects(a));
        System.out.println("a intersects b = " + a.intersects(b));
        System.out.println("a intersects c = " + a.intersects(c));
        System.out.println("a intersects d = " + a.intersects(d));
        System.out.println("b intersects c = " + b.intersects(c));
        System.out.println("b intersects d = " + b.intersects(d));
        System.out.println("c intersects d = " + c.intersects(d));
    }
}